package com.cx.core.magazine.action;

import java.io.InputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

import com.cx.core.magazine.model.bean.Article;
import com.cx.core.magazine.model.bean.form.ArticleForm;
import com.cx.core.magazine.service.MagazineManageService;
import com.cx.core.magazine.util.MagTool;
import com.li.core.actionBase.ActionBase;


public class ArticleManageAction extends ActionBase {
	
	public ActionForward addArticle(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ArticleForm articleForm = (ArticleForm)form;
		FormFile upload = articleForm.getUpload();
		String type = upload.getContentType();
		String filename = upload.getFileName();
		byte[] bytes = upload.getFileData();
		InputStream is = upload.getInputStream();
		int size = upload.getFileSize();
		
		Article article = new Article();
		article.setPeriod_id(articleForm.getPeriod_id());
		article.setContent(articleForm.getContent());
		article.setPublish_date(articleForm.getPublish_date_time().getTime());
		article.setPublisher(articleForm.getPublisher());
		article.setTitle(articleForm.getTitle());
		article.setType(articleForm.getType());
		article.setUser("1");
		
		MagazineManageService ms = (MagazineManageService) super
				.getBean("magazineManageService");
		int imageid = ms.saveImage(filename, bytes);
		article.setImage_id(String.valueOf(imageid));
		ms.saveArticle(article);
		
		String imagename = article.getImage_id()+"_"+filename;
		MagTool.getMagTool().saveFileTolocal(imagename,is,true);
		
		StringBuffer sb = new StringBuffer("{success:true,image_id:'");
		sb.append(imagename);
		sb.append("',id:'");
		sb.append(article.getId());
		sb.append("',publish_date:");
		sb.append(article.getPublish_date());
		sb.append("}");
		response.setCharacterEncoding("utf-8");
		response.getWriter().write(sb.toString());
		return null;
	}
	
	public ActionForward updateArticle(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ArticleForm articleForm = (ArticleForm)form;
		FormFile upload = articleForm.getUpload();
		String type = upload.getContentType();
		String filename = upload.getFileName();
		byte[] bytes = upload.getFileData();
		InputStream is = upload.getInputStream();
		int size = upload.getFileSize();
		
		Article article = new Article();
		article.setId(articleForm.getId());
		article.setPeriod_id(articleForm.getPeriod_id());
		article.setContent(articleForm.getContent());
		article.setPublish_date(articleForm.getPublish_date_time().getTime());
		article.setPublisher(articleForm.getPublisher());
		article.setTitle(articleForm.getTitle());
		article.setType(articleForm.getType());
		article.setImage_id(articleForm.getImage_id().substring(0,articleForm.getImage_id().indexOf("_")));
		article.setUser("1");
		
		MagazineManageService ms = (MagazineManageService) super
				.getBean("magazineManageService");
		
		int imageid = ms.saveImage(filename, bytes);
		if (imageid ==0 ){
			//如果没有上传图片则用原有图片名称填充
			filename = articleForm.getImage_id().substring(articleForm.getImage_id().indexOf("_")+1);
		}else{
			//刪掉原來关联的图片并设置新Id
			ms.deleteImage(article.getImage_id());
			article.setImage_id(String.valueOf(imageid));
		}
		
		ms.updateArticle(article);
		String imagename = article.getImage_id()+"_"+filename;

		MagTool.getMagTool().saveFileTolocal(imagename,is,true);
		StringBuffer sb = new StringBuffer("{success:true,image_id:'");
		sb.append(imagename);
		sb.append("'}");
		response.setCharacterEncoding("utf-8");
		response.getWriter().write(sb.toString());
		return null;
	}
	
	public ActionForward deleteArticle(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ArticleForm articleForm = (ArticleForm)form;
		int id = articleForm.getId();
		String image_id = articleForm.getImage_id().substring(0,articleForm.getImage_id().indexOf("_"));
		MagazineManageService ms = (MagazineManageService) super
				.getBean("magazineManageService");
		ms.deleteArticle(id);
		if ( !image_id.equals("0")){
			ms.deleteImage(image_id);
		}
		return null;
	}
}
